package com.xjnt.mnui.service;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xjnt.frameworks.annotation.Service;
import com.xjnt.frameworks.core.BaseService;
import com.xjnt.mnui.entity.SubjectTemplate;

/** 
* 类说明：模板管理service类
* @author 作者 LUXIN
* @version 创建时间：2015年11月19日 上午11:06:27 
*/
@Service
public class SubjectTemplateService extends BaseService<SubjectTemplate>{

	@Override
	public SubjectTemplate getModel() {
		return SubjectTemplate.me;
	}
	/**
	 * 获取模板列表，查询所有type = 0 即 启用状态的模板
	 * @return List<SubjectTemplate>
	 */
	public List<SubjectTemplate> findTemplateList(){
		String sql = "SELECT t.uuid,t.name from mnui_subject_template t where t.type = ?";
		List<Record> templateList = Db.find(sql, new Object[]{"0"});
		List<SubjectTemplate> list = new ArrayList<SubjectTemplate>();
		for (Record record : templateList) {
			SubjectTemplate model = new SubjectTemplate();
			model.set("uuid", record.get("uuid"));
			model.set("name", record.get("name"));
			list.add(model);
		}
		return list;
	}
	/**
	 * 查询某一模板被专题使用个数
	 * @param id
	 * @return record
	 */
	public Record findTemplateSumById(Object id){
		String sql = "select count(t.uuid) as sum from mnui_subject_template t,mnui_subject s where s.template_uuid = t.uuid and t.uuid = ?";
		Record t = Db.findFirst(sql,new Object[]{id});
		return t;
	}
	/**
	 * 批量启用
	 * @param ids
	 * @return
	 */
	public boolean enables(Object[] ids){
		String sql = "update mnui_subject_template t set t.type = ? where t.uuid = ?";
		for (Object id : ids) {
			Db.update(sql, new Object[]{0,id});
		}
		return true;
	}
	/**
	 * 批量停用
	 * @param ids
	 * @return
	 */
	public boolean disables(Object[] ids){
		String sql = "update mnui_subject_template t set t.type = ? where t.uuid = ?";
		for (Object id : ids) {
			Db.update(sql, new Object[]{1,id});
		}
		return true;
	}
}
 